//
//  LoginRegisterCompanyView.swift
//  GLUZSwift
//
//  Created by xyanl on 2025/3/7.
//  Copyright © 2025 考拉🐨. All rights reserved.
//

import UIKit
import Reusable
import Localize_Swift

extension LoginRegisterCompanyView: NibLoadable {}

class LoginRegisterCompanyView: UIView {
    
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var chineseRequiredLabel: UILabel!
    @IBOutlet weak var ruRequiredLabel: UILabel!
    
    @IBOutlet weak var infoView: UIView!
    @IBOutlet weak var contentTextField: UITextField!
    
    var textDidChangeBlock: ((String) -> Void)?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        infoView.addRoundedCorners(radius: 10)
        chineseRequiredLabel.addRoundedCorners(radius: 8)
        contentTextField.delegate = self
        /// 监听语言的变化
        NotificationCenter.default.addObserver(self, selector: #selector(updateLanguageAction), name: NSNotification.Name(LCLLanguageChangeNotification), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(textFieldTextDidChangeNotification), name: UITextField.textDidChangeNotification, object: contentTextField)
        updateLanguageAction()
    }
    
    // 更新语言
    @objc private func updateLanguageAction() {
        
        titleLabel.text = "公司/个人名称".localized(.login)
        contentTextField.placeholder = "公司/个人名称".localized(.login)
        
        if LanguageManager.isLanguageZH {
            chineseRequiredLabel.isHidden = false
            ruRequiredLabel.isHidden = true
            
        } else {
            chineseRequiredLabel.isHidden = true
            ruRequiredLabel.isHidden = false
        }
        
    }
}

// MARK: - UITextFieldDelegate
extension LoginRegisterCompanyView: UITextFieldDelegate {
    
    @objc func textFieldTextDidChangeNotification(noti: Notification) {
        let textField = noti.object as! UITextField
        textDidChangeBlock?(textField.text!)
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let newText = (textField.text as NSString?)?.replacingCharacters(in: range, with: string) ?? ""
        // 屏蔽掉表情
        if newText.sg.isContainsEmoji {
            return false
        }
        return true
    }
    
}
